<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "cate_attr_val".
 *
 * @property int $id
 * @property int $attr_id 属性ID
 * @property string $val 属性值
 * @property int $addtime 添加时间
 */
class CateAttrVal extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'cate_attr_val';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['attr_id', 'val', 'addtime'], 'required'],
            [['attr_id', 'addtime'], 'integer'],
            [['val'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'attr_id' => '属性ID',
            'val' => '属性值',
            'addtime' => '添加时间',
        ];
    }
    
    
    public static function getCateAttrValByAttrId($attr_id){
        $sql = "select id,val from cate_attr_val where attr_id=$attr_id";
        $command = \Yii::$app->db->createCommand($sql);
        return $data = $command->queryAll();
    }
    
    
    public static function getCateAttrArrByAttrId($attr_id){
        $data = self::getCateAttrValByAttrId($attr_id);
        $result = [];
        foreach ($data as $value){
            $result[$value['id']] = $value['val'];
        }
        return $result;
    }
    
    
    public static function getValInStringByAttrId($attr_id){
        $sql = "select val from cate_attr_val where attr_id=$attr_id";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryAll();
        $result = "";
        if(!empty($data)){
            foreach($data as $value){
                $result .= $value['val']."<br/>";
            }
        }
        return $result;
    }
    
    public static function getValInTextareaByAttrId($attr_id){
        $sql = "select val from cate_attr_val where attr_id=$attr_id";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryAll();
        $result = [];
        if(!empty($data)){
            foreach($data as $value){
                array_push($result,$value['val']);
            }
        }
        return implode("\n", $result);
    }
}
